<template>
  <div class="bottom-bar">
    <div class="bar-item bar-left">
      <div>
        <i class="icon service"></i>
        <span class="text">客服</span>
      </div>
      <div>
        <i class="icon shop"></i>
        <span class="text">店铺</span>
      </div>
      <div @click="like">
        <i :class="{'like':isLike,'icon select':!isLike}" ></i>
        <span class="text" >收藏</span>
      </div>
    </div>
    <div class="bar-item bar-right">
      <div class="cart" @click="addToCart">加入购物车</div>
      <div class="buy" @click="buy">购买</div>
    </div>
    <!-- <sport-ball ref="ball" class="sport-ball"></sport-ball> -->
  </div>

</template>

<script>
  import {addLike} from 'network/like/like.js'

  export default{
    name:'',
    data(){
      return {
        isLike:false,
        sendGood:{}
       }
    },
    computed:{
      // isLike:{
      //   get(){},
      //   set(val){return val}
      // }
    },
    components:{

    },
    methods:{
      addToCart(){
        this.$emit('addGoods')
      },
      like(){
        this.isLike=!this.isLike
        this.$toast.show("收藏成功",2000)
      },
      checkLike(){
        let allGood=JSON.parse(window.sessionStorage.likeGood)
        console.log('??',this.sendGood.goodId);
        if(allGood.filter(item=>item.goodId==this.sendGood.goodId).length){
          this.like()
        }
      },
      buy(){
        this.$emit("sendGoodToBuy")
        this.$router.push("/buy")
      }
    },

    created() {
      this.$bus.$on("activeGood",res=>{
        this.isLike=true
        //!!!
        this.sendGood.goodId=res.iid
        this.sendGood.userId=JSON.parse(window.sessionStorage.user).id
        this.sendGood.price=res.price
        this.sendGood.title=res.title.slice(0,10)
        this.sendGood.num=res.cfav
        this.sendGood.image=res.image||res.show.img
        window.sessionStorage.setItem("activeGood",JSON.stringify(this.sendGood))
        console.log('res',window.sessionStorage.activeGood);
        console.log('created',this.isLike);
      })
    },
    mounted() {
      console.log('mounted',this.isLike);
      // this.like()
    },
    beforeDestroy() {
      console.log('over');
      if(this.isLike){
        let sendGood=window.sessionStorage.activeGood
        addLike(sendGood).then(res=>{
          console.log(res,res.data.result);
          if(res.data.result=="yes"){
            this.$toast.show("收藏成功",2000)
          }
        }).catch(e=>{
          console.log(e);
        })
      }
      //!!!
      // this.sendGood.goodId=res.iid
      // this.sendGood.userId=JSON.parse(window.sessionStorage.user).id
      // this.sendGood.price=res.price
      // this.sendGood.title=res.title.slice(0,10)
      // this.sendGood.num=res.cfav
      // this.sendGood.image=res.image||res.show.img
      // window.sessionStorage.setItem("activeGood",JSON.stringify(this.sendGood))
      // console.log('res',window.sessionStorage.activeGood);
      
    }
  }
</script>

<style scoped>
  .bottom-bar{
    height: 58px;
    position: fixed;
    background-color: #e4eae3;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    text-align: center;
  }
  .bar-item{
    flex: 1;
    display: flex;
  }
  .bar-item>div{
    flex: 1;
  }
  .bar-left .text{
    font-size: 13px;
  }
  .bar-left .icon{
    display: block;
    width: 22px;
    height: 22px;
    margin: 10px auto 3px;
    background: url("~assets/img/detail/detail_bottom.png") 0 0/100%;   /* ??? */
  }
  .bar-left .service {
    background-position:0 -54px;
  }
  .bar-left .shop {
    background-position:0 -98px;
  }
  .bar-right {
    font-size: 15px;color: #fff;
    line-height: 58px;
  }

  .bar-right .cart {
    background-color:#ffe817;
    color: #333;
  }
  .bar-right .buy {
    background-color: #f69;
  }
  .sport-ball {
    position: absolute;
    left: 225px;
    bottom:20px;
  }
  .like{
    display: block;
    width: 22px;
    height: 22px;
    margin: 10px auto 3px;
    background: url("~assets/img/detail/detail_bottom.png") 0 -28px/100%;
  }
</style>
